package com.bat.util;


import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 内部接口返回对象
 * @author zhimingyang
 */
public class R<T> implements Serializable {

    private static final String OK = "";
    private static final String ERROR = "error";

    private boolean success;
    private String message;
    private int code;
    private T data;
    private String err;

    public R<T> success() {
        this.success = true;
        this.message = OK;
        return this;
    }

    public R<T> failure() {
        this.success = false;
        this.message = ERROR;
        return this;
    }

    public R<T> failure(String message) {
        this.success = false;
        this.message = message;
        return this;
    }

    public  static R fail(String message) {
        R r=new R();
        r.success = false;
        r.message = message;
        r.err=message;
        return r;
    }
    public static <T> R<T> fail(int code,String message,T data) {
        R r=new R();
        r.success = false;
        r.message = message;
        r.err=message;
        r.code=code;
        r.data(data);
        return r;
    }
    public static   <T> R<T> fail(int code,String message) {
        R r=new R();
        r.success = false;
        r.message = message;
        r.code=code;
        return r;
    }
    public R<T> data(T data) {
        this.data = data;
        return this;
    }

    public R<T> code(int code) {
        this.code = code;
        return this;
    }

    public static  <T> R<T> s(T t){
        return new R<T>().success().code(HttpStatus.OK.value()).data(t);
    }

    public static  <T> R<T> s(){
        return new R<T>().success().code(HttpStatus.OK.value());
    }

    public R<T> message(String message) {
        this.message = message;
        return this;
    }

    public String getErr() {
        return err;
    }

    public void setErr(String err) {
        this.err = err;
    }

    public int getCode() {
        return code;
    }

    public boolean isSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }


}